package com.web.tags;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.struts.util.LabelValueBean;

import com.web.util.string.StringUtil;

public class BoxTag implements Tag {
	private PageContext pageContext;
	private Tag parent;
	
	public String styleId = "";
	public String styleClass = "";
	public String heading = "";
	public ArrayList<LabelValueBean> items;	
	public String width = "100%";
	
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return SKIP_PAGE;
	}

	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		try {
	         pageContext.getOut().print(getInnerHTML());
	      } catch (IOException e) {
	         throw new JspException("Error: IOException while writing to client" + e.getMessage());
	      }
	      return SKIP_BODY;
	}	

	public void release() {
		// TODO Auto-generated method stub

	}

	public PageContext getPageContext() {
		return pageContext;
	}

	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}

	public Tag getParent() {
		return parent;
	}

	public void setParent(Tag parent) {
		this.parent = parent;
	}

	public String getHeading() {
		return heading;
	}

	public void setHeading(String heading) {
		this.heading = heading;
	}

	public ArrayList<LabelValueBean> getItems() {
		return items;
	}

	public void setItems(ArrayList<LabelValueBean> items) {
		this.items = items;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getStyleId() {
		return styleId;
	}

	public void setStyleId(String styleId) {
		this.styleId = styleId;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}	
	
	public String getInnerHTML() {
		
		String innerHTML = "<table [STYLEIDCLAUSE] [STYLECLASSCLAUSE] width=\"[WIDTH]\"> " +
							"<tr>" +
							"<td class=\"heading\" colspan=\"2\">" +
							"<div class=\"t_heading_ul_\">" +
							"<div class=\"t_heading_ur_\">" +
							"<h4>" + heading + "</h4>&nbsp;" +
							"</div>" +
							"</div>" +
							"</td>" +
							"</tr>";
		
		if (!StringUtil.isNullOrEmpty(styleId)) {
			innerHTML = StringUtil.replace(innerHTML, "[STYLEIDCLAUSE]", "id=\"" + styleId + "\"");		
		}
		else {
			innerHTML = StringUtil.replace(innerHTML, "[STYLEIDCLAUSE]", "");		
		}
		
		if (!StringUtil.isNullOrEmpty(styleClass)) {
			innerHTML = StringUtil.replace(innerHTML, "[STYLECLASSCLAUSE]", "class=\"" + styleClass + "\"");
		}
		else {
			innerHTML = StringUtil.replace(innerHTML, "[STYLECLASSCLAUSE]", "");
		}
		
		if (!StringUtil.isNullOrEmpty(width)) {
			innerHTML = StringUtil.replace(innerHTML, "[WIDTH]", "width=\"" + width + "\"");
		}
		else {
			innerHTML = StringUtil.replace(innerHTML, "[WIDTH]", "");
		}
		
		if (!items.isEmpty()) {
			for (LabelValueBean item: items) {
				innerHTML += "<tr><td class=\"label\">" + item.getLabel() +"</td>" +
						"<td class=\"data\">" + item.getValue() + "</td></tr>";				
			}
		}
		innerHTML += "</table>";
		System.out.println(innerHTML);
		return innerHTML;
	}

}
